package com.lxk.service.impl;

import com.lxk.dao.AdminMapper;
import com.lxk.dao.AsMyMapper;
import com.lxk.pojo.Admin;
import com.lxk.pojo.AdminExample;
import com.lxk.service.AdminService;
import com.lxk.vo.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author JohnSoybean
 * @create 2020/4/13 11:30
 * @description
 */
@Service
@Transactional
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AsMyMapper mm;
    @Autowired
    private AdminMapper am;

    public Integer getTotalPages(Integer num) {
        AdminExample example = new AdminExample();
        Integer count = (int)am.countByExample(example);
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<Admin> selAdminPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<Admin> list = mm.selAdminPage(start, num, sort, order);
        PageModel<Admin> model = new PageModel<Admin>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public Admin selById(Integer adminId) {
        Admin admin = am.selectByPrimaryKey(adminId);
        return admin;
    }

    public void delAdmin(Integer adminId) {
        am.deleteByPrimaryKey(adminId);
    }

    public void addAdmin(Admin admin) {
        am.insertSelective(admin);
    }

    public void modifyAdmin(Admin admin) {
        am.updateByPrimaryKeySelective(admin);
    }

}
